<template>
  <view class="cart-container">
    <!-- 当购物车中有商品时 -->
    <view v-if="getGoods.length>0">
      <my-address></my-address>
      <view class="cart-title ">
        <uni-icons type="shop"
                   size='20'></uni-icons>
        <text class="cart-title-text">购物车</text>
      </view>
      <!-- 使用公共组件my-goods -->
      <uni-swipe-action>
        <uni-swipe-action-item :right-options="options"
                               @click="bindClick(goods.goods_id,$event)"
                               @change="swipeChange($event, index)"
                               v-for="(goods,index) in getGoods"
                               :key="goods.goods_id">
          <my-goods :goods="goods">
            <!-- 使用插槽实现radio效果 -->
            <template #radio>
              <!-- 给radio点击事件,拿到点击的所在商品的id和所在商品取反状态 -->
              <!-- .stop阻止事件冒泡 -->
              <radio :checked="goods.good_number>0?goods.goods_status:false"
                     color="#c00000"
                     @click.stop="changeGoods(goods.goods_id,!goods.goods_status)"></radio>
            </template>
            <template #number>
              <!-- 当给某个对象点击事件时,无法通过直接再点击时间上取消冒泡事件时,再这个点击对象外面套一个父元素,利用父元素阻止冒泡行为 -->
              <view @click.stop="">
                <!-- @change是组件uni-number-box的触发点击事件,但它参数只有一个值 -->
                <!-- 要获取到点击事件的商品id和商品数量 因为这个change事件是组件自己内部自定义的事件,所以强行给这个chang事件给参数会造成取不到当前输入框的值,但是$event是当前点击所产生的值 -->
                <uni-number-box :min="0"
                                :value="goods.good_number"
                                @change='changeGoodsNumber(goods.goods_id,$event)'></uni-number-box>
              </view>

            </template>
          </my-goods>
        </uni-swipe-action-item>
      </uni-swipe-action>
      <!-- 结算组件 -->
      <my-settle></my-settle>
    </view>
    <view class="empty-cart"
          v-else>
      <img src="/static/cart_empty@2x.png"
           alt=""
           class="empty-img">
      <text class="tip-text">空空如也</text>
    </view>
  </view>
</template>
<script>
import setTabBarBridge from '../../mixin/car-tabbar'
import { mapGetters, mapMutations } from 'vuex'
export default ({
  mixins: [setTabBarBridge],
  data () {
    return {
      options: [
        {
          text: '取消',
          style: {
            backgroundColor: '#007aff'
          }
        }, {
          text: '删除',
          style: {
            backgroundColor: '#dd524d'
          }
        }
      ]
    };
  },
  computed: {
    ...mapGetters('cart', ['getGoods'])
  },
  methods: {
    ...mapMutations('cart', ['updateGoodsStatus', 'updateGoodsNumber', "deleteGoods"]),
    // 点击了侧滑按钮
    bindClick (id, e) {
      console.log(e, id);
      // 判断是否点击了删除按钮
      if (e.content.text == '删除') {
        console.log('删除了');
        this.deleteGoods(id)
      }
    },
    swipeChange (value, index) {
      console.log(value, index);
    },
    // 修改商品状态
    changeGoods (goods_id, goods_status) {
      this.updateGoodsStatus({ goods_id, goods_status })
    },
    //点击计数器修改商品的数量
    changeGoodsNumber (goods_id, good_number) {
      // console.log(goods_id, good_number);
      this.updateGoodsNumber({ goods_id, good_number })
    }
  }
})
</script>
<style lang="scss" scoped>
.cart-container {
  padding-bottom: 50px;
  .cart-title {
    height: 40px;
    display: flex;
    align-items: center;
    font-size: 14px;
    padding-left: 5px;
    border-bottom: 1px solid #efefef;
    .cart-title-text {
      margin-left: 10px;
    }
  }
}
.empty-cart {
  display: flex;
  flex-direction: column;
  align-items: center;
  padding-top: 150px;

  .empty-img {
    width: 90px;
    height: 90px;
  }

  .tip-text {
    font-size: 12px;
    color: gray;
    margin-top: 15px;
  }
}
</style>
